package com.microservice.postservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.microservice.commonutils.R;
import com.microservice.commonutils.userVo.UserInfoVo;
import com.microservice.postservice.client.UserInfoClient;
import com.microservice.postservice.entity.PostContent;
import com.microservice.postservice.entity.PostImage;
import com.microservice.postservice.entity.vo.PostContentImageVo;
import com.microservice.postservice.mapper.PostContentMapper;
import com.microservice.postservice.service.PostContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.microservice.postservice.service.PostImageService;
import com.microservice.servicebase.exceptionhandler.MicroException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author leesin
 * @since 2022-05-14
 */
@Service
public class PostContentServiceImpl extends ServiceImpl<PostContentMapper, PostContent> implements PostContentService {

    @Autowired
    private PostImageService postImageService;
    @Autowired
    private PostContentService postContentService;
    @Autowired
    private UserInfoClient userInfoClient;

    @Override
    public List<PostContentImageVo> getPostContentAndImage(String postId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("gmt_create");
        wrapper.eq("id",postId);
        List<PostContent> postContentArrayList= postContentService.list(wrapper);
        List<PostContentImageVo> finalList=new ArrayList<>();
        for (int i = 0; i < postContentArrayList.size(); i++) {
            //每个贴子
            PostContent postContent= postContentArrayList.get(i);
            //封装的VO
            PostContentImageVo postContentImageVo=new PostContentImageVo();
            //将每个贴子放入封装的vo
            BeanUtils.copyProperties(postContent,postContentImageVo);
            List<PostImage> imageList = postImageService.getImageList(postContent.getId());
            //将图片list放入vo
            postContentImageVo.setPostImageList(imageList);
            finalList.add(postContentImageVo);
        }
        return finalList;
    }

    public List<PostContentImageVo> getPostContentAndImageAll() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("gmt_create");
//        wrapper.orderByAsc("gmt_create");

        List<PostContent> postContentArrayList= postContentService.list(wrapper);
        List<PostContentImageVo> finalList=new ArrayList<>();
        for (int i = 0; i < postContentArrayList.size(); i++) {
            //每个贴子
            PostContent postContent= postContentArrayList.get(i);
            //封装的VO
            PostContentImageVo postContentImageVo=new PostContentImageVo();
            //将每个贴子放入封装的vo
            BeanUtils.copyProperties(postContent,postContentImageVo);
            List<PostImage> imageList = postImageService.getImageList(postContent.getId());
            //将图片list放入vo

            UserInfoVo userInfoById = userInfoClient.getUserInfoById(postContent.getUserId());
            postContentImageVo.setUserInfoVo(userInfoById);

            postContentImageVo.setPostImageList(imageList);
            finalList.add(postContentImageVo);
        }
        return finalList;
    }




    @Override
    public boolean addPost(PostContent postContent) {

        boolean save = postContentService.save(postContent);


        // TODO 发帖带图
        if (save){
            return true;
        }else {
            return false;
        }



    }
}
